package com.gym.openapi.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gym.openapi.common.ResponseResult;
import com.gym.openapi.common.ResultTable;
import com.gym.openapi.dao.CustomerMapper;
import com.gym.openapi.entity.Customer;
import com.gym.openapi.services.CustomerServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ming
 * 2021/11/26
 * description:
 */
@Service
public class CustomerServicesImpl implements CustomerServices {

    @Autowired
    private CustomerMapper customerMapper;

/**
 *@author:ming
 *@Date: 2021/11/26
 *@description:分页显示
 *PageHelper.startPage()使用问题：自动添加多余的limit，以及利用PageInfo和Page手动分页
 */
    @Override
    public ResultTable table(Integer page, Integer limit, String name, Integer state) {
        PageHelper.startPage(page, limit);
        List<Customer> customerList = customerMapper.getTable(name, state);
        PageInfo pi = new PageInfo(customerList);
        return new ResultTable(0, "", (int) pi.getTotal(), pi.getList());

    }

    /**
     *@author:ming
     *@Date: 2021/11/26
     *@description:批量删除
     */
    @Override
    public ResponseResult del(Integer[] ids) {
        int i = customerMapper.del(ids);
        if (i > 0) {
            return ResponseResult.getSuccess("解决了一切");
        }
        return ResponseResult.getFail("干不掉我们");
    }

    /**
     *@author:ming
     *@Date: 2021/11/26
     *@description:修改
     */
    @Override
    public ResponseResult update(Customer c) {
        if (c != null) {

            int i = customerMapper.updateByPrimaryKeyWithBLOBs(c);
            if (i > 0) {
                return ResponseResult.getSuccess("改好了");
            }
        }
        return ResponseResult.getFail("修改没好");
    }

    /**
     *@author:ming
     *@Date: 2021/11/26
     *@description:添加
     */
    @Override
    public ResponseResult add(Customer c) {
        if (c != null) {

            int i = customerMapper.insertSelective(c);
            if (i > 0) {
                return ResponseResult.getSuccess("加入成功");
            }
        }
        return ResponseResult.getFail("添加错了");
    }

    @Override
    public List<Customer> getAll() {
        return customerMapper.getAll();
    }
}
